package com.mobile.view.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class GetIpAddress {
    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            strings.add(i + "");
        }
//        for (int i = 0; i < strings.size(); i++) {
//           if(i>3){
//               strings.remove(i);
//           }
//        }
        for (String string : strings) {
            if (Integer.valueOf(string) > 3) {
                strings.remove(string);
            }
        }
    }

    private static String getOuterIp() throws IOException {
        InputStream inputStream = null;
        try {
            URL url = new URL("http://1212.ip138.com/ic.asp");
            URLConnection urlconnnection = url.openConnection();
            inputStream = urlconnnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GB2312");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer webContent = new StringBuffer();
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                webContent.append(str);
            }
            int ipStart = webContent.indexOf("[") + 1;
            int ipEnd = webContent.indexOf("]");
            return webContent.substring(ipStart, ipEnd);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    public static String getInnerIp() {
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            log.error("无法获取本机ip地址：", e);
        }
        String hostAddress = inetAddress.getHostAddress();
        if (hostAddress == null) {
            log.error("无法获取本机ip地址");
        }
        return hostAddress;
    }
}  